#include "fs_user.h"

static FATFS  fs_volumes[FF_VOLUMES];    /* 逻辑磁盘工作区 */
static FS_FIL fs_file_objs[FF_VOLUMES];  /* File object */

/* 文件系统初始化 */
FRESULT fs_user_init(uint8_t TARGETS)
{
    FRESULT FS_RET = (FRESULT) 0;
    char    log_drv_nbr[2];

    /* 根据设置挂载不同设备 */
    if ((TARGETS & FS_DEV_SD) != 0)     /* 挂载SD卡 */
    {
        /* 自动生成盘符 */
        memset(log_drv_nbr, 0, sizeof(log_drv_nbr));
        sprintf(log_drv_nbr, "%d:", FS_SD_VOL_IDX);
        
        /* 立即挂载 */
        FS_RET |= f_mount(&fs_volumes[FS_SD_VOL_IDX], log_drv_nbr, 1);
    }

    if ((TARGETS & FS_DEV_SPIFLASH) != 0)  /* 挂载SPIFLASH卡 */
    {

    }

    return FS_RET;
}

/* 获得文件对象 (对应不同的存储设备) */
FS_FIL * fs_user_get_fileobj(uint8_t TARGET)
{
    FS_FIL * file_obj = NULL;

    if (TARGET & FS_DEV_SD)
    {
        file_obj = &fs_file_objs[FS_SD_VOL_IDX];
    }
    else if (TARGET & FS_DEV_SPIFLASH)
    {
        file_obj = &fs_file_objs[FS_SPIFLASH_VOL_IDX];
    }
    else if (TARGET & FS_DEV_NANDFLASH)
    {
        file_obj = &fs_file_objs[FS_NANDFLASH_VOL_IDX];
    }

    return file_obj;
}
